<?php
/*
 * Created on Feb 19, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
include('../includes/session.php');
include('../models/cityregion_model.php');

class CityRegionController{
	
	function CityRegionController(){
		global $session;
		if(isset($_POST['cityedit'])){
        	$this->cityEdit();
     	}else if(isset($_POST['cityadd'])){
        	$this->cityAdd();
     	}else if(isset($_POST['cityexadd'])){
        	$this->cityExadd();
     	}else if(isset($_POST['cityexedit'])){
        	$this->cityExedit();
     	}else if(isset($_POST['districtadd'])){
        	$this->districtAdd();
     	}else if(isset($_POST['districtedit'])){
        	$this->districtEdit();
     	}else{       		
        	header("Location: ".$session->referrer);
       	}
	}
	
	function cityExadd(){
		global $session, $form;
				
	    $resultValidation = $this->checkCityValidation(
										null,
										$_POST['cityName'], 
										$_POST['citySortNo'],
										$_POST['cityCreateDate']);
	    	
		/* Add Successful */
		if($resultValidation == 0){
	    	$_SESSION['successname'] = $_POST['cityName'];
	        $_SESSION['success'] = true;
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else if($resultValidation == 1){
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
	    }
	    /* Add attempt failed */
	    else if($resultValidation == 2){
	        $_SESSION['successname'] = $_POST['cityName'];
	        $_SESSION['success'] = false;
	        header("Location: ".$session->referrer);
		}
	}
	
	function cityExedit(){
		global $session, $form;
		
		$resultValidation = $this->checkCityValidation(
										$_POST['SelectedCity'],
										$_POST['cityName'], 
										$_POST['citySortNo'],
										null);
		
		/* Account edit successful */
	    if($resultValidation == 0){
	    	$_SESSION['edit'] = true;
			$_SESSION['editname'] = $_POST['cityName'];
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else{
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
		}
	}
	
	function districtAdd(){
		global $session, $form;
				
	    $resultValidation = $this->checkDistrictValidation(
										null,
										$_POST['name'], 
										$_POST['sortNo'],
										$_POST['type'],
										$_POST['createDate']);
	    	
		/* Add Successful */
		if($resultValidation == 0){
	    	$_SESSION['successdistrictname'] = $_POST['name'];
	        $_SESSION['successDistrict'] = true;
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else if($resultValidation == 1){
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
	    }
	    /* Add attempt failed */
	    else if($resultValidation == 2){
	        $_SESSION['successdistrictname'] = $_POST['name'];
	        $_SESSION['successDistrict'] = false;
	        header("Location: ".$session->referrer);
		}
	}
	
	function districtEdit(){
		global $session, $form;
		
		$resultValidation = $this->checkDistrictValidation(
										$_POST['SelectedDistrict'],
										$_POST['name'], 
										$_POST['sortNo'],
										$_POST['type'],
										null);
		
		/* Account edit successful */
	    if($resultValidation == 0){
	    	$_SESSION['editdistrict'] = true;
			$_SESSION['editdistrictname'] = $_POST['name'];
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else{
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
		}
	}
	
	function cityEdit(){
		global $session, $form;
		
		$resultValidation = $this->checkValidation(
										$_POST['SelectedRegion'],
										$_POST['name'], 
										$_POST['sortNo'],
										null);
		
		/* Account edit successful */
	    if($resultValidation == 0){
	    	$_SESSION['edit'] = true;
			$_SESSION['editname'] = $_POST['name'];
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else{
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
		}
	}
	
	function cityAdd(){
		global $session, $form;
				
	    $resultValidation = $this->checkValidation(
										null,
										$_POST['name'], 
										$_POST['sortNo'],
										$_POST['createDate']);
	    	
		/* Add Successful */
		if($resultValidation == 0){
	    	$_SESSION['successname'] = $_POST['name'];
	        $_SESSION['success'] = true;
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else if($resultValidation == 1){
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
	    }
	    /* Add attempt failed */
	    else if($resultValidation == 2){
	        $_SESSION['successname'] = $_POST['name'];
	        $_SESSION['success'] = false;
	        header("Location: ".$session->referrer);
		}
	}
	
	function checkValidation($id = null, $name, $sortno, $createDate){
		global $form, $cityModel;
		/* Username error checking */
      	$field = "name";  //Use field name for username
	    if(!$name || strlen($name = trim($name)) == 0){
	    	$form->setError($field, prnMsg(_('Nhập tên khu đô thị'),'error'));
	    }
				
		if(!$sortno || strlen($sortno = trim($sortno)) == 0){
	    	$sortno = 0;
	    }
		
		/* Errors exist, have user correct them */
	    if($form->num_errors > 0){
	    	return 1;  //Errors with form
	    }/* No errors, add the new account to the */
	    else{
	    	if(!isset($id)){
		        return $cityModel->addNewRegion($name, $sortno, $createDate);					
			}else{
				return $cityModel->editRegion($id, $name, $sortno);
			}
	   	}
	}
	
	function checkCityValidation($id = null, $name, $sortno, $createDate){
		global $form, $cityModel;
		/* Username error checking */
      	$field = "name";  //Use field name for username
	    if(!$name || strlen($name = trim($name)) == 0){
	    	$form->setError($field, prnMsg(_('Nhập tên Tỉnh/TP'),'error'));
	    }
				
		if(!$sortno || strlen($sortno = trim($sortno)) == 0){
	    	$sortno = 0;
	    }
		
		/* Errors exist, have user correct them */
	    if($form->num_errors > 0){
	    	return 1;  //Errors with form
	    }/* No errors, add the new account to the */
	    else{
	    	if(!isset($id)){
		        return $cityModel->addNewCity($name, $sortno, $createDate);					
			}else{
				return $cityModel->editCity($id, $name, $sortno);
			}
	   	}
	}
	
	function checkDistrictValidation($id = null, $name, $sortno, $type, $createDate){
		global $form, $cityModel;
		/* Username error checking */
      	$field = "name";  //Use field name for username
	    if(!$name || strlen($name = trim($name)) == 0){
	    	$form->setError($field, prnMsg(_('Nhập tên Quận/Huyện'),'error'));
	    }
		
		$field = "district";  //Use field name for username
	    if($type <= 0){
	    	$form->setError($field, prnMsg(_('Chọn Tỉnh/TP'),'error'));
	    }
				
		if(!$sortno || strlen($sortno = trim($sortno)) == 0){
	    	$sortno = 0;
	    }
		
		/* Errors exist, have user correct them */
	    if($form->num_errors > 0){
	    	return 1;  //Errors with form
	    }/* No errors, add the new account to the */
	    else{
	    	if(!isset($id)){
		        return $cityModel->addNewDistrict($name, $sortno, $type, $createDate);					
			}else{
				return $cityModel->editDistrict($id, $name, $sortno, $type);
			}
	   	}
	}
};

/* Create userinfo controller object */
$cityRegionController = new CityRegionController;
?>
